package com.jcp.controller;

import static org.hamcrest.CoreMatchers.nullValue;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import com.jcp.entity.UsersLogin;
import com.jcp.service.UsersLoginService;
import com.jcp.serviceimp.UsersLoginServiceImp;
import com.jcp.serviceimp.UsersLoginServiceImp;
import com.jcp.util.FaceIDUtil;
import com.jcp.util.MD5Util;
import com.sun.jmx.snmp.SnmpStringFixed;

/**
 * Servlet implementation class loginservlet
 */
/**
 * 用户登录跳转
 * 
 * @author Administrator 吴祥鑫
 *
 *
 */
@WebServlet("/loginservlet")
public class loginservlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	UsersLoginService us = new UsersLoginServiceImp();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public loginservlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		// response.getWriter().append("Served at: ").append(request.getContextPath());
		// 设置字符集
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		// 获取account userPwd
		String account = null;
		String userPwd = null;
		String op = "";
		UsersLogin userLogin = null;

		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}
		// 刷脸

		if (request.getParameter("op").equals("facelogin")) {
			String userId = request.getParameter("userId");
			userLogin = us.selectById(Integer.parseInt(userId)); 
			
		} else if (request.getParameter("op").equals("login")) {

			account = request.getParameter("account");
			userPwd = request.getParameter("pwd");
			
			userLogin = us.selectByCountAndPwd(account, MD5Util.getEncodeByMd5(userPwd));
		}
		
		// }
		if (userLogin==null) {
			request.getRequestDispatcher("login.jsp").forward(request, response);
		} else if (userLogin.getPosName().equals("部长")) {
			request.getSession().setAttribute("user", userLogin);
			//request.getRequestDispatcher("min.do?op=tb").forward(request, response);
			response.sendRedirect("min.do?op=tb");
		} else if (userLogin.getPosName().equals("员工")) {
			request.getSession().setAttribute("user", userLogin);
		    
			//request.getRequestDispatcher("sta.do?op=").forward(request, response);
			response.sendRedirect("sta.do");
			
			//request.getRequestDispatcher("index.jsp").forward(request,response);
			
		} else if (userLogin.getPosName().equals("人事")) {
			request.getSession().setAttribute("user", userLogin);
			response.sendRedirect("ns.do?op1=zhexiandenglu");
			
		} else {
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
